<template>
  <div class="buildingEdit">
    <el-input
      placeholder="输入关键字进行过滤"
      v-model="filterText"
      style="margin-bottom:10px;">
    </el-input>
    <el-tree
      ref="tree"
      class="filter-tree"
      :data="treeData"
      :props="defaultProps"
      :accordion="true"
      :expand-on-click-node="true"
      :filter-node-method="filterNode"
      :highlight-current="true"
      :render-content="renderContent"
      @node-click="changeFloor"
    >
    </el-tree>
  </div>
</template>

<script>
  import roomApi from '../../service/corporation/roomApi'

  const STRUCTURE_TYPE = {
    BUILDING: "building",
    FLOOR: "floor",
    STANDARD: "standard",
    UNKNOWN: "unknown",
  }

  let typeOfStructure = function(data) { // 判断房间类型
    let type = STRUCTURE_TYPE.UNKNOWN;
    if(data.blName) {
      type = STRUCTURE_TYPE.BUILDING;
    } else if(data.flName) {
      type = STRUCTURE_TYPE.FLOOR;
    } else if(data.stdId) {
      type = STRUCTURE_TYPE.STANDARD;
    }
    return type;
  }

  export default {
    watch: {
      filterText(val) {
        this.$refs.tree.filter(val);
      }
    },

    methods: {
      init() {
        roomApi.getReserveStructure().then(res => {
          this.treeData = res.body.data;
          console.log(this.treeData)
        }, res => {})
      },
      filterNode(value, data) {
        if (!value){
          return true;
        }
        if(typeOfStructure(data) === STRUCTURE_TYPE.BUILDING) {
          return data.blName.indexOf(value) !== -1;
        } else if(typeOfStructure(data) === STRUCTURE_TYPE.FLOOR) {
          return data.flName.indexOf(value) !== -1;
        } else if(typeOfStructure(data) === STRUCTURE_TYPE.STANDARD) {
          return data.stdName.indexOf(value) !== -1;
        } else {
          return false;
        }
      },
      renderContent(h, { node, data }) {
        let countArea = (
          <template></template>
        );
        let labelName = "";
        if(typeOfStructure(data) === STRUCTURE_TYPE.BUILDING) {
          labelName = data.blName;
        } else if(typeOfStructure(data) === STRUCTURE_TYPE.FLOOR) {
          labelName = data.flName;
          countArea = (
            <span style="float: right; margin-right: 20px">{data.count}</span>
          );
        } else if(typeOfStructure(data) === STRUCTURE_TYPE.STANDARD) {
          labelName = data.stdName;
          countArea = (
            <span style="float: right; margin-right: 20px">{data.count}</span>
        );
        }
        return (
          <span>
            <span>
              <span>{labelName}</span>
            </span>
            {countArea}
          </span>);
      },
      changeFloor(data) {
        let floorId = null;
        if(typeOfStructure(data) === STRUCTURE_TYPE.FLOOR || typeOfStructure(data) === STRUCTURE_TYPE.STANDARD) {
          floorId = data.flId;
          this.$emit("changeFloor", floorId);
        }
      },
    },

//    props: {
//      treeData: Array
//    },

    data() {
      return {
        treeData: [],
        filterText: '',
        defaultProps: {
          children: 'children',
          label: 'label'
        }
      };
    },

    mounted() {
      this.init();
    }
  };
</script>

<style>
  .buildingEdit {
    width: 100%;
    float: left;
    padding: 20px 10px;
    background-color: #EFF2F7;
    box-shadow: -1px 2px 5px #cccccc;
  }
  .buildingEdit .el-input{
    width: 100%!important;
  }
</style>
